package servlet;
import bean.Login;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.sql.*;
@WebServlet(name = "Admin_LoginServlet", urlPatterns= "/Admin_LoginServlet")
public class Admin_LoginServlet extends HttpServlet {
    public void init(ServletConfig config)throws ServletException{
        super.init(config);
    }
    public void service(HttpServletRequest request,
                        HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Connection con=null;
        String loginName=request.getParameter("loginName").trim(),
                password=request.getParameter("password").trim();
        try{
            Class.forName("org.mariadb.jdbc.Driver");
            Connection conn= DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system","root","123456");
            Statement stmt=conn.createStatement();
            String sql="select* from hotels where HotelID='"+loginName+"' and Mpassword='"+password+"'";
            ResultSet rs= stmt.executeQuery(sql);
            boolean m=rs.next();
            if (m){
                success(request,response,loginName,password);
                RequestDispatcher dispatcher=request.getRequestDispatcher("admin-Home.jsp");
                dispatcher.forward(request,response);
            }else{
                String backNews="您输入的用户名不存在，或密码不匹配";
                System.out.println(backNews);
                fail(request,response,loginName,backNews);
            }
            con.close();
        }catch(SQLException | ClassNotFoundException exp){
            String backNews="";
            fail(request,response,loginName,backNews);
        }finally{
            try{
                con.close();
            }catch (Exception ee){}
        }}

    public void success(HttpServletRequest request,HttpServletResponse response,String loginName,String password){
        Login ad_login=null;
        HttpSession session=request.getSession(true);
        try{ad_login= (Login) session.getAttribute("ad_login");
            if(ad_login==null){
                ad_login= new Login();
                session.setAttribute("ad_login",ad_login);
            }
            String name=ad_login.getLoginID();
            if(name.equals(loginName)){
               ad_login.setBackNews(loginName+"已经登陆了");
                ad_login.setLoginID(loginName);//输入姓名
                System.out.println("成功写入JAVAbean");
            }else{
                ad_login.setBackNews(loginName+"登陆成功");
                ad_login.setLoginID(loginName);
            }
        }catch (Exception ee){
            ad_login= new Login();
            session.setAttribute("ad_login",ad_login);
            ad_login.setBackNews(ee.toString());
            System.out.println("ee"+ee);
            ad_login.setLoginID(loginName);
        }}
    public void fail(HttpServletRequest request,HttpServletResponse response,String loginName,String backNews){
        response.setContentType("text/html;charset=utf-8");
        try{
            PrintWriter out=response.getWriter();
            out.println("<html><body>");
            out.println("<h2>用户"+loginName+"登陆反馈结果<br>"+backNews+"</h2>");
            System.out.println("12"+backNews);
            out.println("<a href=login.jsp>登录页面</a>");
            out.println("<a href=index.jsp>主页</a>");
            out.println("</body></html>");
        }catch(IOException exp){}
    }
}
